﻿import QtQuick 2.0
import QtQuick.Controls 2.12
import "../utils" as Utils

Rectangle {
    id: sceneRect
    property int currentSceneIndex: 0

    signal startDrive()
    signal backIndex()

    color: "#20212C"
    border.width: 0

    Row{
        x: 35
        y: 34
        spacing: 55
        Repeater{
            id: lightIconRepeater
            model:[
                {"imgSource": "qrc:/loader/imgs/light_3.png",  "value":  can_manager.high_beam_light},
                {"imgSource": "qrc:/loader/imgs/low_light_3.png", "value": can_manager.low_beam_light},
                {"imgSource": "qrc:/loader/imgs/open_light_3.png", "value": can_manager.double_flash}
            ]
            delegate: Image{
                anchors.verticalCenter: parent.verticalCenter
                fillMode: Image.PreserveAspectFit
                source: modelData.value ? modelData.imgSource : lightIconRepeater.getImageSouce(modelData.imgSource, 4)
            }
            function getImageSouce(source, index){
                return source.substring(0, source.lastIndexOf('_')) +"_" + String(index)+ ".png";
            }
        }
    }

    Rectangle{
        id: startDriveBtn
        x: 1088
        y: 29
        width: 160
        height: 55
        radius: 8
        enabled: false
        border.color: Qt.rgba(255, 255, 255, 0.5)
        color:  startDriveBtn.enabled ? "#2997FD" : "#20212C"
        Text {
            anchors.centerIn: parent
            text: "开始驾驶"
            font.family: "Microsoft YaHei"
            font.pixelSize: 24
            font.bold: true
            color: startDriveBtn.enabled ? "white" : "#4D4D56"
        }
        MouseArea{
            anchors.fill: parent
            onClicked: {
                mingDiPopup.open()
            }
        }
    }

    Column{
        x: 862
        y: 242
        spacing: 47
        Utils.MyCheckBtn{
            id: checkOk
            btnNameColor: "white"
            btnNameText: "正常"
            pressColor: "#80DE78"
            onChecked: {
                checkNotOk.isChecked = false
                sceneRepeater.itemAt(sceneRect.currentSceneIndex).preCheckState = 2
                sceneRepeater.itemAt(sceneRect.currentSceneIndex).checkState = 2
                startDriveBtn.enabled = sceneRect.updateStateIndex()
//                recheckTimer.restart()
            }
        }

        Utils.MyCheckBtn{
            id: checkNotOk
            btnNameColor: "#93959C"
            btnNameText: "异常"
            pressColor: "#FE6363"
            onChecked: {
                checkOk.isChecked = false
                sceneRepeater.itemAt(sceneRect.currentSceneIndex).preCheckState = 1
                sceneRepeater.itemAt(sceneRect.currentSceneIndex).checkState = 2
                startDriveBtn.enabled = sceneRect.updateStateIndex()
//                recheckTimer.restart()
            }
        }
    }


    Image{
        id: vehicleImage
        x: 22
        y: 232
        z: 1
        source: "qrc:/loader/imgs/loader_view.png"
        fillMode: Image.PreserveAspectFit
        asynchronous: true
    }

    ListModel{
        id: loaderSceneSources
        ListElement {
            source: "qrc:/loader/imgs/front_view.png"
            x: 22
            y: 121
            z: 2
        }
        ListElement {
            source: "qrc:/loader/imgs/back_view.png"
            x: 259
            y: 121
            z: 2
        }
        ListElement {
            source: "qrc:/loader/imgs/left_view.png"
            x: 157
            y: 253
            z: 2
        }
        ListElement {
            source: "qrc:/loader/imgs/right_view.png"
            x: 157
            y: 160
            z: 2
        }
        ListElement {
            source: "qrc:/loader/imgs/dao_back_view.png"
            x: -77
            y: 121
            z: 0
        }
        ListElement {
            source: "qrc:/loader/imgs/around_view.png"
            x: 92
            y: 121
            z: 0
        }
    }

    Image{
        id: sceneImage
        fillMode: Image.PreserveAspectFit
        x: loaderSceneSources.get(sceneRect.currentSceneIndex).x
        y: loaderSceneSources.get(sceneRect.currentSceneIndex).y
        z: loaderSceneSources.get(sceneRect.currentSceneIndex).z
        source: loaderSceneSources.get(sceneRect.currentSceneIndex).source
    }

    Row{
        x: 35
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 30
        spacing: 20
        Repeater{
            id: sceneRepeater
            model: ["前视野", "后视野", "左视野", "右视野", "铲斗后视野", "360度环视"]
            delegate: Utils.MySceneCheckItem{
                id: sceneCheckItem
                sceneName: modelData
                onSceneSelect:{
                    sceneRect.currentSceneIndex = index 
                    checkOk.isChecked = preCheckState==2 ? true : false
                    checkNotOk.isChecked = preCheckState==1 ? true : false
                    sceneCheckItem.checkState = 1
                }

                Connections{
                    target: sceneRect
                    function onCurrentSceneIndexChanged(){
                        if(sceneRect.currentSceneIndex == index){
                            sceneCheckItem.checkState = 1
                        }
                        else{
                            if(sceneCheckItem.preCheckState == 0){
                                sceneCheckItem.checkState = 0
                            }else{
                                sceneCheckItem.checkState = 2
                            }
                        }
                    }
                }
            }
        }
    }

    Utils.MyPopup{
        parent: parent
        id:myPopup
        onClose_All: {
            backIndex()
            reClear()
        }
    }

    Image {
        x: 1205
        y: 688
        source: "qrc:/imgs/back.png"
        MouseArea{
            anchors.fill: parent
            onClicked: {
                myPopup.open()
            }
        }
    }

    Popup{
        id: mingDiPopup
        width: 634
        height: 338
        closePolicy: Popup.CloseOnEscape
        anchors.centerIn: parent
        background: Rectangle{
            color: "#252A39"
            border.color: "#20FFFFFF"
            border.width: 1
            radius: 8
        }

        Text{
            y: 100
            anchors.horizontalCenter: parent.horizontalCenter
            text: "请鸣笛"
            color: "white"
            font{
                family: "Microsoft YaHei"
                pixelSize: 28
            }
        }

        Rectangle{
            id: okBtn
            y: 200
            width: 193
            height: 67
            anchors.horizontalCenter: parent.horizontalCenter
            radius: 8
            border.width: 0
            color: "dodgerblue"
            Text{
                id: connectText
                anchors.centerIn: parent
                text: "确定"
                color: "white"
                font{
                    family: "Microsoft YaHei"
                    pixelSize: 28
                }
            }
            MouseArea{
                anchors.fill: parent
                onClicked: {
                    recheckTimer.stop()
                    startDrive()
                    reClear()
                    mingDiPopup.close()
                }
            }
        }
    }

    Timer {
        id: recheckTimer
        interval: 60000 * 10;  // 10分钟未驾驶，需重新检查
        running: false;
        repeat: true
        onTriggered: {
            reClear()
        }
    }

    function reClear(){
        for(var i=0; i < sceneRepeater.count; i++){
            sceneRepeater.itemAt(i).checkState = 0
            sceneRepeater.itemAt(i).preCheckState = 0
        }
        sceneRepeater.itemAt(0).checkState = 1
        checkOk.isChecked = false
        checkNotOk.isChecked = false
        startDriveBtn.enabled = false
        sceneRect.currentSceneIndex = 0
    }

    function updateStateIndex(){
        var ok_flag = true
        for(var i=0; i < sceneRepeater.count; i++){
            var item = sceneRepeater.itemAt(i)
            if(item.preCheckState !== 2 ){
                ok_flag = false
            }

            if(item.checkState === 0){
                checkOk.isChecked = false
                checkNotOk.isChecked = false
                sceneRect.currentSceneIndex = i
                return false
            }
        }
        return ok_flag
    }

}
